#include "face_detect_thread.hpp"
#include "face_detect_view.hpp"
#include "face_detect_algo.hpp"

FaceDetectThread::FaceDetectThread(InterfaceSettings &settings)
{
  this->face_detector = std::make_shared<FaceDetectAlgo>();
  this->face_detector->initConfig(settings);
  this->file_path_ = settings.Get_data_path();
}

void FaceDetectThread::run()
{
  auto path = QString::fromStdString(this->file_path_);
  if (path.endsWith(".jpg") || path.endsWith(".png"))
  {
    cv::Mat frame = cv::imread(this->file_path_);
    face_detector->infer_frame(frame);
    emit this->sendResult(frame);
  }
  if (path.endsWith(".mp4"))
  {
    cv::VideoCapture capture(this->file_path_);
    cv::Mat frame;
    while (true)
    {
      capture.read(frame);
      if (frame.empty()) break;
      face_detector->infer_frame(frame);
      emit this->sendResult(frame);
    }
  }
  cv::Mat _Empty;
  emit this->sendResult(_Empty);
}
